module(..., package.seeall)
--[[

===========================================================
Author: Osvaldo Ros (osvaldo@splosive.com)

Copyright (C) 2011 by Splosive Labs (www.splosive.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

===========================================================
]]


--[[
================================================
RDX Button 

	This class illustrates how a reusable component can be created by extending the View class.

	See RDX_Objects for the implementation of the splosive.objects.Button factory which is what enables this component to be added via the declarative syntax like this:

	{type="splosive.objects.Button", id="myButton", upAsset="button_background.png", downAsset="button_background_down.png", label="SEND", fontSize=30, textColor="FFFFFF", imageWidth=200, imageHeight=50}


================================================
]]



function RDX_Button(params)
	
	local protected = {}
	local public = {}
	
	com.splosive.View( nil, public, protected, params )
	
	
	local this = protected
	
	protected.touchBuffer = 5
	
	protected.getLayout = function()
		local data = {
			
			options={
				physics=false
				--coordinatePolicy="contentSize"
			},
			
			objects={
				{type="splosive.objects.Image", id="upAsset", asset=this.params.descriptor.upAsset, imageWidth=this.params.descriptor.imageWidth, imageHeight=this.params.descriptor.imageHeight },
				{type="splosive.objects.Image", id="downAsset", visible=false, asset=this.params.descriptor.downAsset,  imageWidth=this.params.descriptor.imageWidth, imageHeight=this.params.descriptor.imageHeight },
				
				{type="splosive.objects.Text", id="label", text=this.params.descriptor.label, size=this.params.descriptor.fontSize, color=this.params.descriptor.textColor, font=this.params.descriptor.font }
				
			},
			
			behaviors={
			}
		}
		
		return data
	end
	
	
	protected.viewReady = function()
		--com.splosive.trace("Button > ready", "Button")
		this.label.x = this.upAsset.x -- - this.label.width/2
		this.label.y = this.upAsset.y -- - this.label.height/2
		
		Runtime:addEventListener( "touch", this.onScreenTouch )
	end
	
	
	public.clean = function()
		--com.splosive.trace("Button > clean", "View-Allocation")
		Runtime:removeEventListener( "touch", this.onScreenTouch )
		this.super.public.clean()
	end
	
	
	protected.onScreenTouch = function(event)
		--com.splosive.trace("onScreenTouch > phase = " .. event.phase, "Button")
		local left = (this.upAsset.x - this.upAsset.width/2) - this.touchBuffer
		local right = (this.upAsset.x + this.upAsset.width/2) + this.touchBuffer
		local top = (this.upAsset.y - this.upAsset.height/2) - this.touchBuffer
		local bottom = (this.upAsset.y + this.upAsset.height/2) + this.touchBuffer
		
		local currentX, currentY = this.upAsset.parent:contentToLocal(event.x, event.y)
		
		local eventSourceTouched = (currentX >= left and currentX <= right and currentY >= top and currentY <= bottom )
		if(event.phase == "began" and eventSourceTouched)then
			this.upAsset.isVisible = false
			this.downAsset.isVisible = true
		elseif(event.phase == "moved" and eventSourceTouched)then
			this.upAsset.isVisible = false
			this.downAsset.isVisible = true
		elseif(event.phase == "moved" and eventSourceTouched == false)then
			this.upAsset.isVisible = true
			this.downAsset.isVisible = false
		elseif(event.phase == "ended")then
			this.upAsset.isVisible = true
			this.downAsset.isVisible = false
		end
	end
	
	this.init()
	
	return public

end
	

function getView(params)
	return RDX_Button(params)
end

